package com.yupi.lkzyoj.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yupi.lkzyoj.common.BaseResponse;
import com.yupi.lkzyoj.common.ErrorCode;
import com.yupi.lkzyoj.common.ResultUtils;
import com.yupi.lkzyoj.exception.BusinessException;
import com.yupi.lkzyoj.exception.ThrowUtils;
import com.yupi.lkzyoj.judge.service.JudgeService;
import com.yupi.lkzyoj.model.dto.questionsubmit.QuestionSubmitAddRequest;
import com.yupi.lkzyoj.model.dto.questionsubmit.QuestionSubmitCurQueryRequest;
import com.yupi.lkzyoj.model.dto.questionsubmit.QuestionSubmitQueryRequest;
import com.yupi.lkzyoj.model.entity.QuestionSubmit;
import com.yupi.lkzyoj.model.entity.User;
import com.yupi.lkzyoj.model.vo.QuestionSubmitVO;
import com.yupi.lkzyoj.model.vo.QuestionVO;
import com.yupi.lkzyoj.service.QuestionSubmitService;
import com.yupi.lkzyoj.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Lazy;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.concurrent.CompletableFuture;

/**
 * 题目提交接口
 *
 * @author
 * @from
 */
@RestController
@RequestMapping("/question_submit")
@Slf4j
public class QuestionSubmitController {

    @Resource
    private QuestionSubmitService questionSubmitService;

    @Resource
    private UserService userService;

    @Resource
    @Lazy
    private JudgeService judgeService;

    /**
     * 提交题目
     *
     * @param questionSubmitAddRequest
     * @param request
     * @return resultNum 本次提交代码变化数
     */
    @PostMapping("/")
    public BaseResponse<Long> doQuestionSubmit(@RequestBody QuestionSubmitAddRequest questionSubmitAddRequest,
                                               HttpServletRequest request) {
        if (questionSubmitAddRequest == null || questionSubmitAddRequest.getQuestionId() <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        // 登录才能提交代码
        final User loginUser = userService.getLoginUser(request);
        long questionId = questionSubmitAddRequest.getQuestionId();
        Long resultId = questionSubmitService.doQuestionSubmit(questionSubmitAddRequest, loginUser);

        // TODO 提交代码后进入调用判题模块
        CompletableFuture.runAsync(() -> {
            // 调用沙箱获取判题结果
            QuestionSubmit questionSubmit = judgeService.doJudge(resultId);
        });

        return ResultUtils.success(resultId);
    }


    /**
     * 分页获取题目提交列表（非管理员仅能看见非公开信息）
     *
     * @param questionSubmitQueryPageRequest
     * @param request
     * @return {@link BaseResponse}<{@link Page}<{@link QuestionVO}>>
     */
    @PostMapping("/list/page/vo")
    public BaseResponse<Page<QuestionSubmitVO>> listQuestionSubmitByPage(@RequestBody QuestionSubmitQueryRequest questionSubmitQueryPageRequest,
                                                                         HttpServletRequest request) {
        if (questionSubmitQueryPageRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }

        long current = questionSubmitQueryPageRequest.getCurrent();
        long size = questionSubmitQueryPageRequest.getPageSize();
        // 限制爬虫
        ThrowUtils.throwIf(size > 20, ErrorCode.PARAMS_ERROR);
        Page<QuestionSubmit> questionSubmitPage = questionSubmitService.page(new Page<>(current, size),
                questionSubmitService.getQueryWrapper(questionSubmitQueryPageRequest));

        // 进行过滤

        return ResultUtils.success(questionSubmitService.getQuestionSubmitVOPage(questionSubmitPage, request));
    }
    
    @PostMapping("/list/cur")
    public BaseResponse<List<QuestionSubmitVO>> listCurQuestionSubmitByPage(@RequestBody QuestionSubmitCurQueryRequest questionSubmitCurQueryRequest,
                                                                            HttpServletRequest request) {

        User loginUser = userService.getLoginUser(request);
        System.out.println("当前登录用户ID:" + loginUser.getId());
        questionSubmitCurQueryRequest.setUserId(loginUser.getId());
        return ResultUtils.success(questionSubmitService.listCurQuestionSubmitByPage(questionSubmitCurQueryRequest, request));

    }


}
